// 云函数入口文件
const cloud = require('wx-server-sdk')
const TcbRouter = require('tcb-router')
const AipNlpClient = require("baidu-aip-sdk").nlp;
// 设置APPID/AK/SK
const APP_ID = "19238433";
const API_KEY = "gc4TO4SBRGeqGCd7Hf9AzA3T";
const SECRET_KEY = "9P8nltUL4aPvGUpihCwuophQiiDKMHcA";

cloud.init({
  // API 调用都保持和云函数当前所在环境一致
  env: cloud.DYNAMIC_CURRENT_ENV
})
// 云函数入口函数
exports.main = async (event, context) => {
  const app = new TcbRouter({
    event
  })
  const db = cloud.database()
  const wxContext = cloud.getWXContext()
  const client = new AipNlpClient(APP_ID, API_KEY, SECRET_KEY);

  app.router("getKeywords", async (ctx, next) => {
    await client.lexer(event.desc).then(function(res) {
      ctx.body={
        code: 200,
        data: res
      }
      
    }).catch(function(err) {
      // 如果发生网络错误
      return null
    });
    
  
  })

  return app.serve()
}